package com.lavegue.webaction;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.struts2.ServletActionContext;

import com.lavegue.common.BaseAction;
import com.lavegue.common.ListAction;
import com.lavegue.model.BisGroup;
import com.lavegue.model.BisType;
import com.lavegue.model.Comment;
import com.lavegue.model.ManageMsg;
import com.lavegue.service.BisGroupService;
import com.lavegue.service.BisTypeService;
import com.lavegue.service.CommentService;
import com.lavegue.service.ManageMsgService;
import com.lavegue.util.StringUtil;

/**
 * author: luoweibin
 * date:2011-12-21
 * description:
 *
 */
public class IndexAction extends ListAction<ManageMsg> {
	//页面，每行5条
	public static final int ROW_MAX_RECORD = 5;
	protected int pageSize = 20;
	private List<BisType> bisTypes;
	private List<Comment> whyComments;
	private List<Comment> userComments;
	private List<Comment> baodaoComments;
	
	private BisTypeService bisTypeService;
	private BisGroupService bisGroupService;
	private CommentService commentService;
	private ManageMsgService manageMsgService;
	private int typeId;
	private int groupId;
	private List<BisType> typeMsgList;
	private List<BisGroup> groupMsgList; 
	private String likeName;
	/**
	 * 没有登录时跳转的主页，里面带有登陆框
	 * @return
	 */
	public String notLoginIndex() {
		this.setSessionAtrribute("typeId", ServletActionContext.getRequest().getAttribute("typeId"));
		this.setSessionAtrribute("groupId", ServletActionContext.getRequest().getAttribute("groupId"));
		//找出所有分类
		bisTypes = bisTypeService.findAll();
		
		whyComments = commentService.findWhys();
		userComments = commentService.findUserComment();
		baodaoComments = commentService.findBaodaos();
		Comment gonggao = commentService.findNewGongGao();
		
		for (BisType t : bisTypes) {
			if (t.getBisGroups() != null) {
				BisGroup[] gps = new BisGroup[t.getBisGroups().size()];
				t.getBisGroups().toArray(gps);
				Arrays.sort(gps);
				t.setBisGroupList(Arrays.asList(gps));
			}
		}
		this.setRequestAtrribute("gonggao", gonggao);
		return "notLoginIndexPage";
	}
	
	public String notLoginIndexMsgList() {
		//找出所有分类
		bisTypes = bisTypeService.findAll();
		whyComments = commentService.findWhys();
		userComments = commentService.findUserComment();
		baodaoComments = commentService.findBaodaos();
		
		
		for (BisType t : bisTypes) {
			if (t.getBisGroups() != null) {
				BisGroup[] gps = new BisGroup[t.getBisGroups().size()];
				t.getBisGroups().toArray(gps);
				Arrays.sort(gps);
				t.setBisGroupList(Arrays.asList(gps));
			}
		}
		return "msgList";
	}
	
	public String showComment()
	{
		//找出所有分类
		bisTypes = bisTypeService.findAll();
		whyComments = commentService.findWhys();
		userComments = commentService.findUserComment();
		baodaoComments = commentService.findBaodaos();
		for (BisType t : bisTypes) {
			if (t.getBisGroups() != null) {
				BisGroup[] gps = new BisGroup[t.getBisGroups().size()];
				t.getBisGroups().toArray(gps);
				Arrays.sort(gps);
				t.setBisGroupList(Arrays.asList(gps));
			}
		}
		return "footMiddle";
	}
	
	/**
	 * 登录后的主页,没有左边菜单的
	 * @return
	 */
	public String loginedIndex() {
		if (this.getSessionUser() == null) {
			return G_LOGIN_PAGE;
		}
		this.typeMsgList = bisTypeService.findAll();
		for (BisType t : typeMsgList) {
			List<ManageMsg> manageMsgs = t.getManageMsgs();
			for (ManageMsg msg : manageMsgs) {
				//0 全部，1普通会员，1 vip
				if (t.getUseUsertype() == 0 || t.getUseUsertype() ==1) {
					msg.setCanSend(true);
				} else {
					//1 是vip
					if (this.getSessionUser().getType() == 1) {
						msg.setCanSend(true);
					}
				}
			}
			
			if (this.getSessionUser().getType() == 1) {//1是vip
				List<ManageMsg> newManageMsgs = new ArrayList<ManageMsg>();
				for (ManageMsg msg : manageMsgs) {
					if (msg.getBisGroup().getCompany() == null) {
						newManageMsgs.add(msg);
					} else if (msg.getBisGroup().getCompany().getCompanyName().equals(this.getSessionUser().getCompany().getCompanyName())) {
						newManageMsgs.add(msg);
					}
				}
				t.setManageMsgs(newManageMsgs);
			}
			
		}
		return "mainIndex";
	}
	
	public String findByTypeAndGroup() {
		BisType t = null;
		BisGroup group = null;
		if (typeId != 0) {
			 t = bisTypeService.findById(typeId);
			this.setRequestAtrribute("typeObj", t);
		} else if (groupId != 0) {
			group = bisGroupService.findById(groupId);
			t = group.getBisType();
			this.setRequestAtrribute("typeObj", group);
		}
		if (!StringUtil.isEmpty(likeName)) {
			if(likeName.contains("%")) {
				try {
					likeName = URLDecoder.decode(likeName, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
		pageObj = manageMsgService.findPageByTypeAndGroup(typeId, groupId, pageNo, pageSize, this.getSessionUser(), likeName);
		if (this.getSessionUser() != null) {
			List<ManageMsg> data = pageObj.getData();
			for (ManageMsg m : data) {
				if (t != null) {
					//0 全部，1普通会员，1 vip
					if (t.getUseUsertype() == 0 || t.getUseUsertype() ==1) {
						m.setCanSend(true);
					} else {
						//1 是vip
						if (this.getSessionUser().getType() == 1) {
							m.setCanSend(true);
						}
					}
				} else {
					//0 全部，1普通会员，1 vip
					if (m.getBisType().getUseUsertype() == 0 || m.getBisType().getUseUsertype() ==1) {
						m.setCanSend(true);
					} else {
						//1 是vip
						if (this.getSessionUser().getType() == 1) {
							m.setCanSend(true);
						}
					}
				}
			}
		}
		return "msgPage";
	}
	
	public List<Comment> getWhyComments() {
		return whyComments;
	}

	public List<BisGroup> getGroupMsgList() {
		return groupMsgList;
	}

	public void setGroupMsgList(List<BisGroup> groupMsgList) {
		this.groupMsgList = groupMsgList;
	}

	public List<BisType> getTypeMsgList() {
		return typeMsgList;
	}

	public void setTypeMsgList(List<BisType> typeMsgList) {
		this.typeMsgList = typeMsgList;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}
	
	public void setWhyComments(List<Comment> whyComments) {
		this.whyComments = whyComments;
	}

	public List<Comment> getUserComments() {
		return userComments;
	}

	public void setUserComments(List<Comment> userComments) {
		this.userComments = userComments;
	}

	public List<Comment> getBaodaoComments() {
		return baodaoComments;
	}



	public void setBaodaoComments(List<Comment> baodaoComments) {
		this.baodaoComments = baodaoComments;
	}

	public ManageMsgService getManageMsgService() {
		return manageMsgService;
	}

	public void setManageMsgService(ManageMsgService manageMsgService) {
		this.manageMsgService = manageMsgService;
	}

	public List<BisType> getBisTypes() {
		return bisTypes;
	}

	public void setBisTypes(List<BisType> bisTypes) {
		this.bisTypes = bisTypes;
	}



	public BisTypeService getBisTypeService() {
		return bisTypeService;
	}

	public void setBisTypeService(BisTypeService bisTypeService) {
		this.bisTypeService = bisTypeService;
	}

	public BisGroupService getBisGroupService() {
		return bisGroupService;
	}

	public void setBisGroupService(BisGroupService bisGroupService) {
		this.bisGroupService = bisGroupService;
	}

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public String getLikeName() {
		return likeName;
	}

	public void setLikeName(String likeName) {
		this.likeName = likeName;
	}
	
	
}

